import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  server: {
    host: '0.0.0.0',
    port: 3000,
    open: true,
  },
  // 分类型输出到子目录，并禁止小资源内联
  build: {
    assetsDir: 'assets',
    cssCodeSplit: true,
    assetsInlineLimit: 0, // 禁止图片等被 base64 内联，全部生成独立文件
    rollupOptions: {
      output: {
        entryFileNames: 'assets/js/[name]-[hash].js',
        chunkFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: (assetInfo) => {
          const ext = path.extname(assetInfo.name || '').slice(1)
          if (ext === 'css') return 'assets/css/[name]-[hash][extname]'
          if (['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'avif'].includes(ext)) {
            return 'assets/img/[name]-[hash][extname]'
          }
          if (['woff', 'woff2', 'ttf', 'eot', 'otf'].includes(ext)) {
            return 'assets/fonts/[name]-[hash][extname]'
          }
          return 'assets/static/[name]-[hash][extname]'
        },
      },
    },
  },
  optimizeDeps: {
    esbuildOptions: {},
  },
})
